package com.swz.dcrm.ui.mine;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.flyco.dialog.listener.OnOperItemClickL;
import com.flyco.dialog.widget.ActionSheetDialog;
import com.google.gson.Gson;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Group;
import com.swz.dcrm.model.Input;
import com.swz.dcrm.model.Page;
import com.swz.dcrm.model.PersonalInfo;
import com.swz.dcrm.model.PersonalInfoUpdateVo;
import com.swz.dcrm.model.Role;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.viewmodel.GroupViewModel;
import com.swz.dcrm.ui.viewmodel.MainViewModel;
import com.swz.dcrm.ui.viewmodel.StaffViewModel;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class StaffInformationFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.rg_sex)
    RadioGroup rgSex;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_post)
    TextView tvPost;

    @BindView(R.id.tv_group)
    TextView tvGroup;

    @Inject
    GroupViewModel groupViewModel;
    @Inject
    MainViewModel mainViewModel;
    @Inject
    StaffViewModel staffViewModel;


    private ActionSheetDialog actionSheetDialog;

    private List<Group> groups;
    private List<Role> roles;
    PersonalInfoUpdateVo personalInfoUpdateVo=new PersonalInfoUpdateVo();

    public static StaffInformationFragment newInstance() {

        return new StaffInformationFragment();
        // Required empty public constructor
    }
    @Override
    public void onLoadRetry() {
        groupViewModel.getGroups(1,1000)
                .observe(this, pageBaseResponse -> {
                   if(pageBaseResponse.isSuccess()){
                       groups=pageBaseResponse.getData().getList();
                   }
                });
        mainViewModel.getRoles().observe(this, listBaseResponse -> {
             if(listBaseResponse.isSuccess()){
                 roles=listBaseResponse.getData();
             }
        });
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }
    @Override
    public int layoutId() {
        return R.layout.fragment_staff_information;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        String str=StaffInformationFragmentArgs.fromBundle(getArguments()).getPersonalInfo();
        etPhone.setOnFocusChangeListener((v, hasFocus) -> {
            if(hasFocus){
                mainViewModel.getInput().setValue(new Input(v,0,true));
            }else {
                mainViewModel.getInput().setValue(new Input(v,0,false));
            }
        });
        etName.setOnFocusChangeListener((v, hasFocus) -> {
            if(hasFocus){
                mainViewModel.getInput().setValue(new Input(v,0,true));
            }else {
                mainViewModel.getInput().setValue(new Input(v,0,false));
            }
        });
        rgSex.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId){
                case R.id.rb_boy:
                    personalInfoUpdateVo.setSex(1);
                    break;
                case R.id.rb_girl:
                    personalInfoUpdateVo.setSex(0);
                    break;
            }
        });
        if(!str.equals("test")){
            title.setText(getString(R.string.staff_information_title1));
            PersonalInfo  personalInfo=new Gson().fromJson(str,PersonalInfo.class);
            personalInfoUpdateVo.setId(personalInfo.getId());
            if(!TextUtils.isEmpty(personalInfo.getName())){
                etName.setText(personalInfo.getName());
            }
            if(!TextUtils.isEmpty(personalInfo.getPhone())){
                etPhone.setText(personalInfo.getPhone());
            }
            if(!TextUtils.isEmpty(personalInfo.getRole().getRoleGroup())){
                tvPost.setText(personalInfo.getRole().getRoleGroup());
            }
           if(personalInfo.isBoy()){
               rgSex.check(R.id.rb_boy);
           }else {
               rgSex.check(R.id.rb_girl);
           }
            if(!TextUtils.isEmpty(personalInfo.getGroupName())){
                tvGroup.setText(personalInfo.getGroupName());
            }
        }else {
            title.setText(getString(R.string.staff_information_title));
            rgSex.check(R.id.rb_boy);
        }

        tvRight.setText(getString(R.string.staff_information_save));

        return true;
    }


    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }
    @OnClick(R.id.tv_right)
    public void save(){
       String name =etName.getText().toString().trim();
       if(TextUtils.isEmpty(name)){
           showToast(getString(R.string.add_customer_hint)+getString(R.string.staff_information_name));
           return;
       }else {
           personalInfoUpdateVo.setName(name);
       }
       String phone=etPhone.getText().toString().trim();
        if(TextUtils.isEmpty(phone)){
            showToast(getString(R.string.add_customer_hint)+getString(R.string.staff_information_phone));
            return;
        }else {
            personalInfoUpdateVo.setPhone(phone);
        }
        if(TextUtils.isEmpty(tvPost.getText().toString())){
            showToast(getString(R.string.add_customer_hint)+getString(R.string.staff_information_post));
            return;
        }
        if(TextUtils.isEmpty(tvGroup.getText().toString())){
            showToast(getString(R.string.add_customer_hint)+getString(R.string.staff_information_group));
            return;
        }
        if(personalInfoUpdateVo.getId()==0){
            staffViewModel.getStaff().removeObservers(this);
            staffViewModel.addStaff(personalInfoUpdateVo)
                    .observe(this, personalInfoBaseResponse -> {
                        showToast(personalInfoBaseResponse.getMsg());
                    });
        }else {
            staffViewModel.getStaff().removeObservers(this);
            staffViewModel.saveStaff(personalInfoUpdateVo)
                    .observe(this, personalInfoBaseResponse -> {
                        showToast(personalInfoBaseResponse.getMsg());
                    });
        }

    }

    @OnClick({R.id.c_group,R.id.tv_post})
    public void click(View view){
        List<String> list=new ArrayList<>();
        String[] s = new String[]{};
        switch (view.getId()){
            case R.id.c_group:

                for(Group group:groups){
                    list.add(group.getName());
                }

                actionSheetDialog = new ActionSheetDialog(getContext(), list.toArray(s), null);
                actionSheetDialog.setOnOperItemClickL((parent, view1, position, id) -> {
                    tvGroup.setText(list.get(position));
                    personalInfoUpdateVo.setGroupId(groups.get(position).getId());
                    actionSheetDialog.dismiss();
                });
                break;
            case R.id.tv_post:
                for(Role role:roles){
                    list.add(role.getRoleGroup());
                }
                actionSheetDialog = new ActionSheetDialog(getContext(), list.toArray(s), null);
                actionSheetDialog.setOnOperItemClickL((parent, view1, position, id) -> {
                    tvPost.setText(list.get(position));

                        personalInfoUpdateVo.setRoleId(roles.get(position).getId());

                    actionSheetDialog.dismiss();
                });
                break;

        }
        actionSheetDialog.isTitleShow(false);
        actionSheetDialog.show();
    }
}
